/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.CompanyCode;
import org.crystalrange.enterprise.model.FiscalPeriodVariant;
import org.crystalrange.enterprise.service.CompanyCodeService;
import org.crystalrange.model.Company;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Window;

public class CompanyCodeController extends BaseController {

    protected CompanyCodeService companyCodeService;
    protected CompanyCode companyCode;

    public CompanyCodeService getCompanyCodeService() {
        return this.companyCodeService;
    }

    public void setCompanyCodeService(CompanyCodeService companyCodeService) {
        this.companyCodeService = companyCodeService;
    }

    public CompanyCode getCompanyCode() {
        return this.companyCode;
    }

    public void setCompanyCode(CompanyCode companyCode) {
        this.companyCode = companyCode;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.companyCode = this.companyCodeService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Company Code: " + this.companyCode.getName());
        } else {
            this.companyCode = this.companyCodeService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Company Code");
        }
    }

    public void onOpen$bandboxCompany(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/companySelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            Company selectedCompany = (Company) this.execution.getAttribute("selectedCompany");
            this.companyCode.setCompany(selectedCompany);
        }

        this.binder.loadAll();
    }

    public void onOpen$bandboxFiscalPeriodVariant(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/fiscalPeriodVariantSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            FiscalPeriodVariant selectedFiscalPeriodVariant = (FiscalPeriodVariant) this.execution.getAttribute("selectedFiscalPeriodVariant");
            this.companyCode.setFiscalPeriodVariant(selectedFiscalPeriodVariant);
        }

        this.binder.loadAll();
    }

    public void onClick$menuitemCompanyCodeSave(Event event) throws Exception {
        this.companyCodeSave();
    }

    public void onClick$toolbarbuttonCompanyCodeSave(Event event) throws Exception {
        this.companyCodeSave();
    }

    private void companyCodeSave() throws Exception {
        this.companyCodeService.save(this.companyCode,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
